import java.awt.Font;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import TextGeneration.Unification;

import components.Children;
import components.Component;
import components.Leaf;
import components.Phrase;

import ruletree.Folder;
import ruletree.RuleTree;
import ruletree.rule.Rule;
import ruletree.rule.RuleList;
import ruletree.rule.Word;
import components.InputXMLDocument;

import view.grammardevelopment.InputXMLDocumentPanel;
import lexicon.Form;
import lexicon.Lexicon;
import lexicon.LexiconList;
import managers.LexiconManager;
import managers.RuleManager;
import managers.SemanticsManager;
import controller.Controller;
import features.Feature;
import features.FeatureList;

public class Driver {

	//System.out.println(rule.getRuleList().get(0).getRuleName());
		//rule.getSpecifiedRule(0).saveRule(f);
		//rule.saveRule(i);
		
		public static void main(String[] args){
			try{
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			}catch(Exception e){}
			
			
			Font oldLabelFont = UIManager.getFont("Label.font");
			UIManager.put("Label.font", oldLabelFont.deriveFont(Font.PLAIN,(float)14));
			
			ArrayList<RuleList> posList = new ArrayList<>();
			File file = new File("Rules\\CombinedRules.xml");
			
			SAXBuilder builder = new SAXBuilder();
			Document document;
			try {
				document = builder.build(file);
				Element ruletree = document.getRootElement();
				RuleList ruleList = new RuleList();
					List<Element> rules = ruletree.getChildren();
					for(Element rule: rules){
						Rule node = Rule.createInstance(rule);
						ruleList.addRule(node);
					}
					
					posList.add(ruleList);
					
				String name = "CL1";		
				InputXMLDocument inputSemRep = SemanticsManager.readSemanticsDocumentFromFile(new File("Rules\\"+name+".xml"));
				
				ArrayList<Component> fromXML = inputSemRep.getClauses();

				System.out.println(" ");
				

				System.out.println("Before applying the rules");


				for(Component x : fromXML){
					System.out.println(x.getName());
					ArrayList<Feature> f1 = x.getFeatureList();
					if(f1 != null)
						for(Feature myFeat : f1)
							System.out.println(myFeat.getName() + " " +myFeat.getValue());
					}
				
				Component modified = Unification.applyRules(ruleList, fromXML.get(0));
				Element resultComponent = modified.generateXMLElement();


				File resultfile = new File("result"+name+ ".xml");
				resultfile.createNewFile();

				XMLOutputter xmlOutput = new XMLOutputter();
				xmlOutput.setFormat(Format.getPrettyFormat());

	
				xmlOutput.output(resultComponent, new FileWriter(resultfile));
				JOptionPane.showMessageDialog(null, "Successfully Saved '"
								+ resultfile.getName() + "'", "Save Success!",
								JOptionPane.INFORMATION_MESSAGE);
				
				
			} catch (JDOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch(Exception e){
				e.printStackTrace();
			}
			
			
		//	RuleTreeNode rootNode = RuleManager.getInstance().getRootNode();
		//	RuleManager.getInstance().editGroupName((RuleGroup)rootNode, "Rulesededes");

		//new Controller().start();
	}
}
